//获取应用实例
const app = getApp()
import api from '../../utils/api'

Page({
  data: {
    list: [],
    pageNumber: 1,
    startDate:'', 
    endDate:''
  },
  onLoad: function (options) {
    let startDate = options.startDate || '';
    let endDate = options.endDate || '';
    console.log(startDate,endDate)
    this.setData({ startDate, endDate})
    this.loadData();
  },
  onShow: function () {
    let food = app.globalData.evaluate;
    let sfpj = app.globalData.sfpj;
    if (sfpj ==1){
      this.data.list.filter(item => {
        if (food.order_no == item.order_no) {
          item.score_state = 'y';
        }
      })
    }
   
    this.setData({
      list:this.data.list
    })
  },
  evaluate:function(e){
    let food = e.currentTarget.dataset.food;
    app.globalData.evaluate=food;
    app.globalData.sfpj=0;
    wx.navigateTo({
      url: '../evaluate/evaluate',
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let user = wx.getStorageSync("user");
    var _this = this;
    let pageNumber = _this.data.pageNumber + 1;
    let param = { "patientId": user.bh, "pageNumber": pageNumber };
    api.POST({
      url: 'history.action',
      params: param,
      success: function (res) {
        let success = res.data.success;
        if (success == "true") {
          let list = _this.data.list;
          let listPull = _this.category(res.data.data.rows);
          for (var i = 0; i < listPull.length;i++){
            list.push(listPull[i])
          }
          _this.setData({ list: list })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.data.msg,
          })
        }
      },
      fail: function () {
        wx.showToast({
          icon: 'none',
          title: '请求失败',
        })
      },
    })
    _this.setData({ pageNumber: pageNumber++})
  },
  loadData(){
    let _this = this;
    let user = wx.getStorageSync("user");
    let { startDate, endDate } = this.data;
    let param = { "patientId": user.bh, "pageNumber": this.data.pageNumber, "startDate": startDate, "endDate": endDate};
    api.POST({
      url: 'history2.action',
      params: param,
      success: function (res) {
        let success = res.data.success;
        if (success == "true") {
          console.log(res.data.data.rows)
          _this.setData({ list: _this.category(res.data.data.rows)})
        } else {
          wx.showToast({
            icon: 'none',
            title: res.data.msg,
          })
        }
      },
      fail: function () {
        wx.showToast({
          icon: 'none',
          title: '请求失败',
        })
      },
    })
  },
  category:function(list){
    for(var i=0;i<list.length;i++){
      let zaocan = [];
      let wucan = [];
      let wancan = [];
      for (var j = 0; j < list[i].details.length; j++) {
        if (list[i].details[j].meals =="早餐"){
          zaocan.push(list[i].details[j]);
        }
        if (list[i].details[j].meals == "午餐") {
          wucan.push(list[i].details[j]);
        }
        if (list[i].details[j].meals == "晚餐") {
          wancan.push(list[i].details[j]);
        }
      }
      list[i].zcList = zaocan;
      list[i].wcList = wucan;
      list[i].ycList = wancan;
    }
    return list;
  },
  selectDate:function(){
    wx.navigateTo({
      url: '../search/search',
    })
  }
})